package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.dto.ExamConfirmDto
import cn.exrick.xboot.base.entity.EmployeeOfficialExamRecord
import cn.exrick.xboot.base.entity.EnterpriseEmployee
import cn.exrick.xboot.base.entity.EnterpriseUser
import cn.exrick.xboot.base.entity.ExamConfirmCheck
import cn.exrick.xboot.base.mapper.ExamConfirmCheckMapper
import cn.exrick.xboot.base.service.*
import cn.exrick.xboot.base.vo.EmployeeOfficialExamVo
import cn.exrick.xboot.base.vo.ExamConfirmCheckDetailVo
import cn.exrick.xboot.base.vo.ExamConfirmCheckListVo
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import cn.exrick.xboot.core.service.UserService
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*

/**
 * 考试监督员考试确认审核接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class IExamConfirmCheckServiceImpl : ServiceImpl<ExamConfirmCheckMapper?, ExamConfirmCheck?>(), IExamConfirmCheckService {


    @Autowired
    private lateinit var iEmployeeOfficialExamRecordService: IEmployeeOfficialExamRecordService

    @Autowired
    private lateinit var securityUtil: SecurityUtil

    @Autowired
    private lateinit var iEnterpriseUserService: IEnterpriseUserService

    @Autowired
    private lateinit var iSecurityAdvisorJobTrainPartService: ISecurityAdvisorJobTrainPartService

    @Autowired
    private lateinit var iExamRoomService: IExamRoomService



    override fun getExamConfirmCheckStatusByOfficialId(examOfficialExamId: String): Boolean? {

        return getOne(KtQueryWrapper(ExamConfirmCheck::class.java).eq(ExamConfirmCheck::officialExamRecordId, iEmployeeOfficialExamRecordService.getMyEmployeeOfficialExamLast()?.id ))?.confirmMessage
    }

    override fun createExamConfirmCheck(examConfirmDto: ExamConfirmDto): Result<Any?>? {

       /* if (examConfirmDto.score!! < 100) {

            return ResultUtil.error(202, "考试没有合格(小于100分)不允许提交审核!")
        }*/

        if (ToolUtil.isNotEmpty(list(KtQueryWrapper(ExamConfirmCheck::class.java)
                        .eq(ExamConfirmCheck::supervisionUserId, examConfirmDto.supervisionUserId)
                        .eq(ExamConfirmCheck::confirmMessage, false)
                        .eq(ExamConfirmCheck::createBy, securityUtil.currUser.id)))) {

            return ResultUtil.error(204, "不可重复提交!")
        }

        //添加正式考试记录
        val employeeOfficialExamRecord = EmployeeOfficialExamRecord()
        iEmployeeOfficialExamRecordService.save(employeeOfficialExamRecord.apply {

            enterpriseId = iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil.currUser.id)
            employeeUserId = securityUtil.currUser.id

            iSecurityAdvisorJobTrainPartService.getById(examConfirmDto.trainPartId)?.let {

                jobTrainPartId = it.id
                jobTrainId = it.jobTrainId
            }
            examCompleteTime = Date()
            supervisionUserId = examConfirmDto.supervisionUserId
            examRoomId = iExamRoomService.getExamRoomLastByAreaId(securityUtil.currUser.areaId)?.id
        })

        //添加审核记录
        save(ExamConfirmCheck().apply {

            officialExamRecordId = employeeOfficialExamRecord.id
            supervisionUserId = examConfirmDto.supervisionUserId
        })

        return ResultUtil.success("添加成功！")
    }

    override fun getExamConfirmBySupervisionId(pageVo: PageVo, confirmMessage: Boolean?): Result<MutableMap<String, Any?>>? {

        return ToolUtil.setListToNul(list(KtQueryWrapper(ExamConfirmCheck::class.java)
                .eq(ExamConfirmCheck::supervisionUserId, securityUtil.currUser.id)
                .eq(ExamConfirmCheck::confirmMessage, confirmMessage)
                .orderByDesc(ExamConfirmCheck::createTime)))?.run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull {
                iEmployeeOfficialExamRecordService.getEmployeeOfficialExamVo(it?.officialExamRecordId!!)?.apply {
                    id = it.id
                }
            })

            ResultUtil.data(result, "获取考试审核列表成功！")
        } ?: ResultUtil.error(201, "暂无数据")

    }

    override fun getExamConfirmDetail(id: String): Result<EmployeeOfficialExamVo?>? {

        return getById(id)?.run {

            ResultUtil.data(iEmployeeOfficialExamRecordService.getEmployeeOfficialExamVo(officialExamRecordId!!)?.apply {
                this.id = id
            }, "获取详情成功！")
        }
    }

    override fun checkExam(id: String?): Result<Any?>? {

        return try {

            getById(id)?.run {

                confirmMessage = true
                checkUserId = securityUtil.currUser.id
                updateById(this)

                ResultUtil.success("审核成功")
            }
        } catch ( e: Exception) {

            ResultUtil.error(e.message)
        }
    }

}